package com.cx.base.day19_thread;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author: robert.song
 * @Date: Created in 2021/11/27
 */
public class Thread_13_9_原子性_Unsafe {
    public static void main(String[] args) {
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            Unsafe unsafe = (Unsafe) theUnsafe.get(null);    //传入的参数是实例，表示该实例的字段，但当前为静态字段的获取，因此参数设为null
            Class c = Class.forName("java.lang.Class");
            try {
                Method m = c.getDeclaredMethod("forName", String.class);
                Class c2 = (Class) m.invoke(c, "java.lang.Class");
                System.out.println(c == c2);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
